package ogl.vecmathimp;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

import ogl.vecmath.Vector;

public class VectorImp implements Vector, Comparable<Vector>{

	
	public float [] vector;
	

	public VectorImp(){
		this.vector = new float[] {0,0,0};
	}
	
	public VectorImp(float a, float b, float c){
		this.vector = new float[] {a,b,c};
	}
	
	@Override
	public float x() {
		return vector[0];
	}

	@Override
	public float y() {
		return vector[1];
	}

	@Override
	public float z() {
		return vector[2];
	}

	@Override
	public Vector add(Vector v) {
		return new VectorImp(x() + v.x(), y() + v.y(), z() + v.z());
	}

	@Override
	public Vector sub(Vector v) {
		return new VectorImp(x() - v.x(), y() - v.y(), z() - v.z());
	}

	@Override
	public Vector mult(float s) {
		return new VectorImp(x() * s, y() * s, z() * s);
	}

	@Override
	public Vector mult(Vector v) {
		return new VectorImp(x() * v.x(), y() * v.y(), z() * v.z());
	}

	@Override
	public float length() {
		return (float) Math.sqrt(x()*x() + y()*y() + z()*z());
	}

	@Override
	public Vector normalize() {
		return mult(1.0f/length());
	}

	@Override
	public float dot(Vector v) {
		return x() * v.x() + y() * v.y() + z() * v.z();
	}

	@Override
	public Vector cross(Vector v) {
		return new VectorImp(y() * v.z() - z() * v.y(), -x() * v.z() + z() * v.x(), x() * v.y() - y() * v.x());
	}

	@Override
	public float[] asArray() {
		return this.vector;
	}

	@Override
	public FloatBuffer asBuffer() {
		FloatBuffer b = BufferUtils.createFloatBuffer(size());
		b.put(x());
		b.put(y());
		b.put(z());
		b.rewind();
		return b;
	}

	@Override
	public void fillBuffer(FloatBuffer buf) {
		buf.put(x());
		buf.put(y());
		buf.put(z());
	}

	@Override
	public int compareTo(Vector o) {
		if (x() != o.x())
		      return (x() < o.x() ? -1 : 1);
		    if (y() != o.y())
		      return (y() < o.y() ? -1 : 1);
		    if (z() != o.z())
		      return (z() < o.z() ? -1 : 1);
		    return 0;
	}

	 @Override
	  public boolean equals(final Object o) {
	    if (!(o instanceof Vector))
	      return false;
	    final Vector v = (Vector) o;
	    return x() == v.x() && y() == v.y() && z() == v.z();
	  }
	
	@Override
	public int size() {
		return getSize();
	}
	
	@Override
	public String toString(){
		return "(" + x() + ", " + y() + ", " + z() + ")";
	}
	

	public static int getSize(){
		return 3;
	}
}
